<?php

	class PicAction extends AdminAction{
			//搜索
		Public function so(){
				$Pic=M('Pic');
				
				//搜索标题
				$so['title']=array('like','%'.$_GET['title'].'%');
				//返回内容
				$result=$this->_page($Pic,10,$so);
				
				$this->assign('list',$result);

				//输出select栏目选项
				$typelist=$this->typeselect(3);
				$this->assign('typelist',$typelist);

				$this->display('index');

		}

        Public function index(){
			$pic=M('Pic');
			$menu=M('Menu');
			$admin=M('Admin');
			
			import('ORG.Util.Page');				//导入分页类
			$count=$pic->where()->count();			//获得符合条件的数据总数
			$Page=new Page($count,5);				//实例化分页类 传入数据总数		
			//定义分页样式
			$Page->setConfig('prev','上页');
			$Page->setConfig('next','下页');
			$Page->setConfig('first','首页');
			$Page->setConfig('last','末页');
			$Page->rollPage = 20;
			$Page->setConfig('theme','%first% %upPage% %linkPage% %downPage% %end%');
			 
			$show=$Page->show();						//分页显示输出

			//获得内容列表
			$list=$pic->order(array('id'=>desc))->limit($Page->firstRow.','.$Page->listRows)->select();
		
			for($i=0,$j=count($list);$i<$j;$i++){
				$list[$i]['menuname']=$menu->where(array('typeid'=>$list[$i]['typeid']))->field('name')->find(); 	//获得栏目名
				$list[$i]['zuozhe']=$admin->where(array('uid'=>$list[$i]['uid']))->field('username')->find();		//获得作者
			}
			
			$this->assign('page',$show);	//输出分页
			$this->assign('list',$list);

			//输出select栏目选项
			$typelist=$this->typeselect(2);
			$this->assign('typelist',$typelist);
            $this->display();
        }
		
		//发布页
		Public function add(){
			$pic=D('Pic');

			$menu=new MenuModel;
			//仅输出文章模型的栏目
			$res=$menu->menulist(2);
			//按顺序遍历出栏目ID和栏目名
			$list=menus($res);
						
			$addtime=date('Y-m-d H:i:s',time());
			
			$path=new AttachmentModel;

			$pics=$path->path(4);	//组图保存目录
			$thumb=$this->litimg();
			$this->assign('thumb',$thumb);
			$this->assign('pics',$pics['path']);

			$this->assign('stick',stick());
			$this->assign('disabled',disabled());
			$this->assign('menu',$list);			//输出栏目列表
			$this->assign('addtime',$addtime);		//输出添加时间
			$this->assign('groupids',groupids());	//输出浏览权限
			$this->display();

		}
		
		//添加至数据库
		Public function adding(){

		}
		
		//编辑页
		Public function edit(){
			$pic=D('Pic');
			$menu=M('Menu');
			
			$menu=new MenuModel;
			//仅输出文章模型的栏目
			$res=$menu->menulist(2);
			//按顺序遍历出栏目ID和栏目名
			$list3=menus($res);
			
			//查询内容
			$list=$pic->relation(true)->find($_GET['id']);

			if($list['pic_attach']){
				$list=array_merge($list,$list['pic_attach']);unset($list['pic_attach']);
			}
			
			//处理基本信息
			$list['content']=stripslashes($list['content']);		
			$list['addtime']=date("Y-m-d H:i:s",$list['addtime']);
			$stick=explode(',',$list['stick']);

			//缩略图		
			$thumb=$this->litimg($list['pic']);
			$this->assign('thumb',$thumb);

			$path=new AttachmentModel;	
			$pics=$path->path(4);	//组图保存目录
			$this->assign('pics',$pics['path']);

			//获得组图缩略图
			$j=count($list['picurl']);
			for($i=0;$i<$j;$i++){
				$pics=explode('/',$list['picurl'][$i]['pics']);
				$pics[1]='s_'.$pics[1];
				$list['picurl'][$i]['s_pics']=implode('/',$pics);
			}
			
			$this->assign('time',time());
			$this->assign('disabled',disabled());					//栏目下拉菜单
			$this->assign('stick',stick());							//输出自定义属性
			$this->assign('stickvalue',$list['stick']);				//输出自定义属性值
			$this->assign('menu',$list3);							//输出栏目
			$this->assign('menuvalue',$list['typeid']);				//栏目默认值
			$this->assign('groupids',groupids());					//输出浏览权限
			$this->assign('groupidsvalue',$list['groupids_view']);	//浏览权限默认值
			$this->assign('allow',allow());							//是否允许评论
			$this->assign('allowvalue',$list['allow_comment']);		//评论默认值
			$this->assign('list',$list);
			$this->display();
		}
		
		//更新流程 
		Public function editing(){
			$pic=D('Pic');

			if(empty($_POST['pic'])){
				unset($_POST['pic']);
			}
			$_POST['allow_comment']=$_POST['allow_comment'][0];

			//处理POST数据
			$_POST=$this->_data($_POST,'pic_attach','picurl');
			$data=$_POST;

			$result=$pic->relation(true)->save($data);

			if($result){
				$this->success('编辑成功!','index');
			}else{
				$this->error('编辑失败！');
			}

		}
 
 		//图片上传
 		public function uploadify(){
 			$res=$this->pic_upload($_POST);
 			echo $res;
 		}


		//删除组图
		public function delpics(){
			//组图保存目录
			$atta=new AttachmentModel;
			$list=$atta->path(4);
			//组图文件名
			$file=$list['path'].$_POST['pics'];

			$s_file=explode('/',$file);
			$s_file[2]='s_'.$s_file[2];
			$s_file=implode('/',$s_file);
			$m_file=explode('/',$file);
			$m_file[2]='m_'.$m_file[2];
			$m_file=implode('/',$s_file);

			if(is_file('../Uploads/'.$file)){
				$res1=unlink('../Uploads/'.$file);
			}
			if(is_file('../Uploads/'.$s_file)){
				$res2=unlink('../Uploads/'.$s_file);
			}
			if(is_file('../Uploads/'.$m_file)){
				$res3=unlink('../Uploads/'.$m_file);
			}

			if(true){
				$picurl=M('Picurl');
				$res=$picurl->where(array('pics'=>$_POST['pics']))->delete();
				$this->ajaxReturn($res);
				//echo 1;
			}else{	
				$this->ajaxReturn('请检查目录权限');
			}
			

		}


}
